package com.foreveredu.propms.web.struts.action;

import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foreveredu.propms.domain.ChargeItem;
import com.foreveredu.propms.domain.District;
import com.foreveredu.propms.domain.PageModel;
import com.foreveredu.propms.domain.SubDistrict;
import com.foreveredu.propms.service.IChargeService;
import com.foreveredu.propms.service.IManagementService;
import com.foreveredu.propms.service.IPropQueryService;
import com.foreveredu.propms.web.struts.actionform.IndividualChargeActionForm;
import com.foreveredu.propms.web.util.ExportExcelUtils;

public class IndividualChargeAction extends AbstractBaseDispatchAction  {
	
	private IManagementService managementService = null;
	private IPropQueryService propQueryService = null;
	private IChargeService chargeService = null;
	private int subId;//查询小区
	private int year;//查询年
	private int month;//查询月份
	private int chargeItemId;//查询收费项
	
	// 获得日志记录器
	Logger log = Logger.getLogger(AccountAction.class);

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}

	public IPropQueryService getPropQueryService() {
		return propQueryService;
	}

	public void setPropQueryService(IPropQueryService propQueryService) {
		this.propQueryService = propQueryService;
	}

	public IChargeService getChargeService() {
		return chargeService;
	}

	public void setChargeService(IChargeService chargeService) {
		this.chargeService = chargeService;
	}
	
	// 跳转到单项收费明细表查询条件页面
	public ActionForward queryIndividualCharge(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

		//获得所有的物业服务处
		List<District> distList = managementService.findDistricts();
		request.setAttribute("distList", distList);
		
		//获得所有收费项目列表
		List<ChargeItem> itemList = chargeService.findChargeItems();
		request.setAttribute("itemList", itemList);
		
		return mapping.findForward("query_individualCharge");
	}
	
	//分页的方法
	public ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		IndividualChargeActionForm inform = (IndividualChargeActionForm) form;
		int distId = inform.getDistId();
		subId = inform.getSubId();//小区id
		year = inform.getYear();
		month = inform.getMonth();
		chargeItemId = inform.getChargeItemId();//收费项目
		ChargeItem chargeItem = chargeService.findChargeItemById(chargeItemId);
		
		SubDistrict sub=new SubDistrict();
		
		sub.setId(subId);//获得页面传入的小区Id
		int offset = 0;
		String pageOffset = request.getParameter("pager.offset");// 获得翻页参数，即下一开始的纪录位置
		if (pageOffset != null && !pageOffset.equals("")) {
			offset = Integer.parseInt(pageOffset);
		}
		if (offset < 0) {
			return mapping.findForward("doQuery_individualCharge");//单项收费表
		}
		//当管理员不选择物业服务处，则查询所有业主当前欠费明细
		PageModel pm = new PageModel();
		pm = propQueryService.queryIndividualCharge(sub, year, month, offset, chargeItemId);
		request.setAttribute("pm", pm);
		request.setAttribute("year", year);
		request.setAttribute("month", month);
		request.setAttribute("subId", subId);
		request.setAttribute("chargeItem", chargeItem);
		request.setAttribute("chargeItemId", chargeItemId);
		
		return mapping.findForward("doQuery_individualCharge");//汇总
	}
	
	//将数据生成到Excel表格中并导出
	public ActionForward doExportExcel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
		ChargeItem chargeItem = chargeService.findChargeItemById(chargeItemId);
		SubDistrict sub=new SubDistrict();
		sub.setId(subId);//获得页面传入的小区Id
		List<Map<String, Object>> queryList = propQueryService.queryIndividualChargeIntoExcel(sub, year, month, chargeItemId);
		try{
			response.setContentType("octets/stream"); 
			//设置表名
			String fileName="单项收费查询("+year+"年"+month+"月)";
			response.addHeader("Content-Disposition","attachment;filename="+new String(fileName.getBytes("GBK"),"ISO8859-1")+".xls"); 
			
            OutputStream out = response.getOutputStream();
            
            //设置表格中的字段名
            String chargeItemName = chargeItem.getName();//获得查询项的名称
            String[] headers = {"业主",chargeItemName};
            //设置Key，然后通过这个Key去Map数组中取值
            String[] columns = {"ownerName","resultCharge"};
            
            ExportExcelUtils.exportExcel("单项收费查询表", headers, columns, queryList, out, "");
            out.close();
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
}





















